package com.kotiln.lgc.sunnyweather.ui.place

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.core.widget.addTextChangedListener
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.LinearLayoutManager
import com.kotiln.lgc.sunnyweather.R
import com.kotiln.lgc.sunnyweather.activity.MainActivity
import com.kotiln.lgc.sunnyweather.activity.WeatherActivity
import com.kotiln.lgc.sunnyweather.base.BaseFragment
import com.kotiln.lgc.sunnyweather.databinding.FragmentPlaceBinding
import com.kotiln.lgc.sunnyweather.util.LogUtil

/**
 *@fileName PlaceFragment
 *@date2020/11/30  13:58
 *@author PlaceFragment$
 *@describe TODO
 *@desc
 */
class PlaceFragment :BaseFragment<FragmentPlaceBinding>() {

     val viewModel by lazy {
        ViewModelProviders.of(this).get(PlaceViewModel::class.java)
    }

    private lateinit var adapter:PlaceAdapter


    override fun initData() {
        if(activity is MainActivity && viewModel.isPlaceSaved()){
            val place =viewModel.getSavedPlace()
            val intent = Intent(context,WeatherActivity::class.java).apply{
                putExtra("location_lng",place.location.lng)
                putExtra("location_lat",place.location.lat)
                putExtra("place_name",place.name)
            }
            startActivity(intent)
            activity?.finish()
            return
        }
        val layoutManager=LinearLayoutManager(activity)
        binding.recyclerView.layoutManager=layoutManager
        adapter=PlaceAdapter(this,viewModel.placeList)
        binding.recyclerView.adapter=adapter

        binding.searchPlaceEdit.addTextChangedListener {
                editable->
            val content=editable.toString()
            if(content.isNotEmpty()){
                viewModel.searchPlaces(content)
            }else{
                binding. recyclerView.visibility=View.GONE
                binding.bgImageView.visibility=View.VISIBLE
                viewModel.placeList.clear()
                adapter.notifyDataSetChanged()
            }
        }

        viewModel.placeLiveData.observe(this, Observer { result ->
            val places = result.getOrNull()
            LogUtil.i("PlaceFragment", places.toString())
            if (places != null) {
                binding.recyclerView.visibility = View.VISIBLE
                binding.bgImageView.visibility = View.GONE
                viewModel.placeList.clear()
                viewModel.placeList.addAll(places)
                adapter.notifyDataSetChanged()
            } else {
                Toast.makeText(activity, "未能查询到任何地址", Toast.LENGTH_SHORT).show()
                result.exceptionOrNull()?.printStackTrace()
            }
        })
    }

    override fun getViewBinding(
        inflater: LayoutInflater,
        container: ViewGroup?
    ): FragmentPlaceBinding = FragmentPlaceBinding.inflate(inflater,container,false)


}